// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.randomizedWorld.randomizedDeadSurvivor;

import zombie.characters.IsoZombie;
import zombie.core.Rand;
import zombie.iso.BuildingDef;
import zombie.iso.IsoDirections;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoWorld;
import zombie.iso.RoomDef;
import zombie.iso.areas.IsoRoom;
import zombie.iso.objects.IsoBarricade;
import zombie.iso.objects.IsoDeadBody;
import zombie.iso.objects.IsoDoor;
import zombie.network.GameServer;

public final class RDSZombieLockedBathroom extends RandomizedDeadSurvivorBase {
    @Override
    public void randomizeDeadSurvivor(BuildingDef def) {
        IsoDeadBody isoDeadBody = null;

        for (int int0 = 0; int0 < def.rooms.size(); int0++) {
            RoomDef roomDef = (RoomDef)def.rooms.get(int0);
            IsoGridSquare isoGridSquare0 = null;
            if ("bathroom".equals(roomDef.name)) {
                if (IsoWorld.getZombiesEnabled()) {
                    IsoGridSquare isoGridSquare1 = IsoWorld.instance.CurrentCell.getGridSquare(roomDef.getX(), roomDef.getY(), roomDef.getZ());
                    if (isoGridSquare1 != null && isoGridSquare1.getRoom() != null) {
                        IsoRoom isoRoom = isoGridSquare1.getRoom();
                        isoGridSquare1 = isoRoom.getRandomFreeSquare();
                        if (isoGridSquare1 != null) {
                            zombie.VirtualZombieManager.instance.choices.clear();
                            zombie.VirtualZombieManager.instance.choices.add(isoGridSquare1);
                            IsoZombie isoZombie = zombie.VirtualZombieManager.instance
                                .createRealZombieAlways(IsoDirections.fromIndex(Rand.Next(8)).index(), false);
                            zombie.ZombieSpawnRecorder.instance.record(isoZombie, this.getClass().getSimpleName());
                        }
                    }
                }

                for (int int1 = roomDef.x - 1; int1 < roomDef.x2 + 1; int1++) {
                    for (int int2 = roomDef.y - 1; int2 < roomDef.y2 + 1; int2++) {
                        isoGridSquare0 = IsoWorld.instance.getCell().getGridSquare(int1, int2, roomDef.getZ());
                        if (isoGridSquare0 != null) {
                            IsoDoor isoDoor = isoGridSquare0.getIsoDoor();
                            if (isoDoor != null && this.isDoorToRoom(isoDoor, roomDef)) {
                                if (isoDoor.IsOpen()) {
                                    isoDoor.ToggleDoor(null);
                                }

                                IsoBarricade isoBarricade = IsoBarricade.AddBarricadeToObject(isoDoor, isoGridSquare0.getRoom().def == roomDef);
                                if (isoBarricade != null) {
                                    isoBarricade.addPlank(null, null);
                                    if (GameServer.bServer) {
                                        isoBarricade.transmitCompleteItemToClients();
                                    }
                                }

                                isoDeadBody = this.addDeadBodyTheOtherSide(isoDoor);
                                break;
                            }
                        }
                    }

                    if (isoDeadBody != null) {
                        break;
                    }
                }

                if (isoDeadBody != null) {
                    isoDeadBody.setPrimaryHandItem(super.addWeapon("Base.Pistol", true));
                }

                return;
            }
        }
    }

    private boolean isDoorToRoom(IsoDoor isoDoor, RoomDef roomDef) {
        if (isoDoor != null && roomDef != null) {
            IsoGridSquare isoGridSquare0 = isoDoor.getSquare();
            IsoGridSquare isoGridSquare1 = isoDoor.getOppositeSquare();
            return isoGridSquare0 != null && isoGridSquare1 != null
                ? isoGridSquare0.getRoomID() == roomDef.ID != (isoGridSquare1.getRoomID() == roomDef.ID)
                : false;
        } else {
            return false;
        }
    }

    private boolean checkIsBathroom(IsoGridSquare isoGridSquare) {
        return isoGridSquare.getRoom() != null && "bathroom".equals(isoGridSquare.getRoom().getName());
    }

    private IsoDeadBody addDeadBodyTheOtherSide(IsoDoor isoDoor) {
        Object object = null;
        if (isoDoor.north) {
            object = IsoWorld.instance.getCell().getGridSquare((double)isoDoor.getX(), (double)isoDoor.getY(), (double)isoDoor.getZ());
            if (this.checkIsBathroom((IsoGridSquare)object)) {
                object = IsoWorld.instance.getCell().getGridSquare((double)isoDoor.getX(), (double)(isoDoor.getY() - 1.0F), (double)isoDoor.getZ());
            }
        } else {
            object = IsoWorld.instance.getCell().getGridSquare((double)isoDoor.getX(), (double)isoDoor.getY(), (double)isoDoor.getZ());
            if (this.checkIsBathroom((IsoGridSquare)object)) {
                object = IsoWorld.instance.getCell().getGridSquare((double)(isoDoor.getX() - 1.0F), (double)isoDoor.getY(), (double)isoDoor.getZ());
            }
        }

        return RandomizedDeadSurvivorBase.createRandomDeadBody(
            ((IsoGridSquare)object).getX(), ((IsoGridSquare)object).getY(), ((IsoGridSquare)object).getZ(), null, Rand.Next(5, 10)
        );
    }

    public RDSZombieLockedBathroom() {
        this.name = "Locked in Bathroom";
        this.setChance(5);
    }
}
